package cn.sylinx.redis.springboot;

import java.util.Map;

import redis.clients.jedis.BinaryJedisPubSub;

public class RedisBinaryPubSub extends BinaryJedisPubSub {

	private Map<byte[], SubscribeMessageHandler<byte[]>> channelsWithHandlers;

	public Map<byte[], SubscribeMessageHandler<byte[]>> getChannelsWithHandlers() {
		return channelsWithHandlers;
	}

	public void setChannelsWithHandlers(Map<byte[], SubscribeMessageHandler<byte[]>> channelsWithHandlers) {
		this.channelsWithHandlers = channelsWithHandlers;
	}

	public RedisBinaryPubSub() {

	}

	public RedisBinaryPubSub(Map<byte[], SubscribeMessageHandler<byte[]>> channelsWithHandlers) {
		this.channelsWithHandlers = channelsWithHandlers;
	}

	public void onMessage(byte[] channel, byte[] message) {
		channelsWithHandlers.get(channel).handle(message);
	}

	public byte[][] getChannels() {
		byte[][] channels = new byte[channelsWithHandlers.size()][];
		channelsWithHandlers.keySet().toArray(channels);
		return channels;
	}
}
